package com.centaline.androidsalesblog.rx.database;

import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.model.businissobject.HistoryBo;
import com.centaline.androidsalesblog.model.businissobject.HistoryInDateBo;
import com.centaline.androidsalesblog.model.databaseobject.EsfEstateDo;
import com.centaline.androidsalesblog.model.databaseobject.FpHistoryDo;
import com.centaline.androidsalesblog.model.databaseobject.NewEstActDo;
import com.centaline.androidsalesblog.model.databaseobject.NewEstDo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.utils.DateUtil;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;

/**
 * 查询足迹记录
 * Created by guilin on 16/2/2.
 */
final class FpHistoryOnSubscribe implements Observable.OnSubscribe<ArrayList<HistoryInDateBo>> {

    private ArrayList<String> dateArrayList = new ArrayList<>();

    @Override
    public void call(Subscriber<? super ArrayList<HistoryInDateBo>> subscriber) {
        final ArrayList<HistoryInDateBo> result = new ArrayList<>();
        final List<FpHistoryDo> fpHistoryDos = DataSupport.where("cityCode = ?", AppConstant.CityCode)
                .order("date desc").find(FpHistoryDo.class);
        HistoryBo historyBo = null;
        for (FpHistoryDo fpHistoryDo : fpHistoryDos) {
            final int type = fpHistoryDo.getType();
            final String resourceId = fpHistoryDo.getResourceId();
            final String date = DateUtil.format(fpHistoryDo.getDate(), DateUtil.FORMAT4);
            switch (type) {
                case 0:
                case 1:
                    historyBo = new HistoryBo(DataSupport.where("postId = ?", resourceId).find(PostDo.class).get(0),
                            resourceId, fpHistoryDo.getDate());
                    break;
                case 2:
                    historyBo = new HistoryBo(DataSupport.where("CestCode = ?", resourceId).find(EsfEstateDo.class).get(0),
                            resourceId, fpHistoryDo.getDate());
                    break;
                case 3:
                    historyBo = new HistoryBo(DataSupport.where("EstExtId = ?", resourceId).find(NewEstDo.class).get(0),
                            resourceId, fpHistoryDo.getDate());
                    break;
                case 4:
                    historyBo = new HistoryBo(DataSupport.where("ActId = ?", resourceId).find(NewEstActDo.class).get(0),
                            resourceId, fpHistoryDo.getDate());
                    break;
                default:
                    break;
            }
            if (!dateArrayList.contains(date)) {
                dateArrayList.add(date);
                result.add(new HistoryInDateBo(fpHistoryDo.getDate()));
            }
            result.get(dateArrayList.indexOf(date)).getList().add(historyBo);
        }
        if (!subscriber.isUnsubscribed()) {
            subscriber.onNext(result);
        }
        subscriber.onCompleted();
    }
}
